import { getRoleList } from '@/api/system/role';
import {modifyUserStatus} from '@/api/system/account'
import { BasicColumn } from '@/components/Table';
import { FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';
import { useMessage } from '@/hooks/web/useMessage';
import { uploadFiles } from '@/api/upload/upload';
type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'account',
    slots: { customRender: 'no' },
  },
  {
    title: 'UserId',
    dataIndex: 'userid',

  },
  {
    title: '手机号码',
    dataIndex: 'phoneNum',

  },
  {
    title: '邮箱',
    dataIndex: 'email',

  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: ({ text }) => {
      //时间戳格式化
      const date = new Date(text);
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      const hour = date.getHours();
      const minute = date.getMinutes();
      const second = date.getSeconds();
      return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${
        hour < 10 ? '0' + hour : hour
      }:${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`;
    }
  },
  {
    title: '角色',
    dataIndex: 'role.roleName',

  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      console.log(record)
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: CheckedType) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          const { createMessage } = useMessage();
          modifyUserStatus(record.userid, newStatus)
            .then(() => {
              record.status = newStatus;
              createMessage.success(`已成功修改用户状态`);
            })
            .catch(() => {
              createMessage.error('修改用户状态失败');
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'account',
    label: '用户名',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'phoneNum',
    label: '手机号',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const accountFormSchema: FormSchema[] = [
  {
    field: 'avatar',
    component: 'Upload',
    label: '头像',
    colProps: {
      span: 12,
    },
    rules: [{ required: false, message: '请选择上传图片' }],
    componentProps: () => {
      return {
        api: uploadFiles,
        accept: ['image/*', 'jpg', 'jpeg', 'png', 'bmp'], //接受文件类型
        maxSize: 20,
        maxNumber: 1,
        onChange: async (e: any) => {},
      };
    },
  },
  {
    field: 'account',
    label: '用户名',
    component: 'Input',
    // helpMessage: ['本字段演示异步验证', '不能输入带有admin的用户名'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
    ],
  },
  {
    field: 'Password',
    label: '密码',
    component: 'InputPassword',
    required: true,
    ifShow: true,
  },
  {
    field: 'confirmPassword',
    label: '重复密码',
    component: 'InputPassword',
    required: false,
    ifShow: true,
  },
  {
    label: '角色',
    field: 'roleId',
    component: 'ApiSelect',
    componentProps: {
      api: getRoleList,
      labelField: 'roleName',
      valueField: 'id',
    },
    required: true,
  },
  {
    field: 'deptName',
    label: '所属部门',
    component: 'TreeSelect',
    componentProps: {
      fieldNames: {
        label: 'name',
        value: 'value',
      },
      getPopupContainer: () => document.body,
    },
    required: true,
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    required: true,
  },

  {
    label: '手机号',
    field: 'phoneNum',
    component: 'Input',
    required: true,
  }
];
